#include "failedpagewidget.h"
#include <ukui-log4qt.h>
#include <gsettings.hpp>
#include "include/theme.h"

FailedPageWidget::FailedPageWidget(QWidget *parent) :
    QWidget(parent),
    m_failedPageIcon(new QLabel()),
    m_failedPageText(new QLabel()),
    m_failedPageButton(new QPushButton()),
    m_failedPageVLayout(new QVBoxLayout())
{
    setupGui();
    initConnect();
    initTheme();

}
void FailedPageWidget::initTheme(){
    if (isDarkTheme()) {
        themeColor = "black";
        m_failedPageIcon->setPixmap(QPixmap(":/default-connect-page/detect_fail_dark.svg"));
    } else {
        themeColor = "white";
        m_failedPageIcon->setPixmap(QPixmap(":/default-connect-page/detect_fail_light.svg"));
    }
}
bool FailedPageWidget::isDarkTheme()
{
    QString systemTheme = kdk::kabase::Gsettings::getSystemTheme().toString();

    if (systemTheme == STYLE_NAME_KEY_DARK || systemTheme == STYLE_NAME_KEY_BLACK) {
        return true;
    } else {
        return false;
    }
}
void FailedPageWidget::setupGui()
{
    m_failedPageIcon->setFixedSize(FailedPageIconSize);

    m_failedPageText->setEnabled(false);
    m_failedPageText->setText(tr("Not detect scanners, please connect scanners firstly!"));

    m_failedPageButton->setMinimumSize(FailedPageButtonSize);
    m_failedPageButton->setText(tr("Connect"));
    m_failedPageButton->adjustSize();
    m_failedPageButton->setFocusPolicy(Qt::StrongFocus);
    m_failedPageButton->setFocus();
    m_failedPageButton->setDefault(true);

//    m_failedPageButton->setStyleSheet("QPushButton{background-color:rgb(55,144,250);color:rgb(255,255,255);}");
    QPalette palette;
    QColor color(55, 144, 250);
    palette.setColor(QPalette::Button, color);
    QColor colorFont(255, 255, 255);
    palette.setColor(QPalette::ButtonText,colorFont);
    m_failedPageButton->setPalette(palette);

    m_failedPageVLayout->setSpacing(0);
    m_failedPageVLayout->addStretch();
    m_failedPageVLayout->addWidget(m_failedPageIcon, 0,  Qt::AlignCenter);
    m_failedPageVLayout->addSpacing(13);
    m_failedPageVLayout->addWidget(m_failedPageText, 0,  Qt::AlignCenter);
    m_failedPageVLayout->addSpacing(29);
    m_failedPageVLayout->addWidget(m_failedPageButton, 0,  Qt::AlignCenter);
    m_failedPageVLayout->addStretch();
    m_failedPageVLayout->setContentsMargins(0, 0, 0, 0);

    setLayout(m_failedPageVLayout);

}

void FailedPageWidget::initConnect()
{
    connect(m_failedPageButton, &QPushButton::clicked, this, [=](){
        emit scanButtonClicked();
    });
    connect(kdk::kabase::Gsettings::getPoint(), &kdk::kabase::Gsettings::systemFontSizeChange,this,&FailedPageWidget::fontSizeChanged);
    connect(kdk::kabase::Gsettings::getPoint(), &kdk::kabase::Gsettings::systemThemeChange, this, &FailedPageWidget::initTheme);
}

void FailedPageWidget::fontSizeChanged()
{
    int systemFontSize = kdk::kabase::Gsettings::getSystemFontSize().toInt();
    QFont font;
    font.setPointSize(systemFontSize);
    m_failedPageButton->setFont(font);

}

